2ad00c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012-2014 the original author or authors.
+ * Copyright 2012-2017 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,9 +15,14 @@
  */
 package org.springframework.data.rest.core.event;
 
+import lombok.EqualsAndHashCode;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.slf4j.Logger;
@@ -26,6 +31,7 @@
import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.ApplicationEvent;
 import org.springframework.context.ApplicationListener;
+import org.springframework.core.annotation.AnnotationAwareOrderComparator;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.data.rest.core.annotation.HandleAfterCreate;
 import org.springframework.data.rest.core.annotation.HandleAfterDelete;
@@ -165,37 +171,50 @@
public class AnnotatedEventHandlerInvoker implements ApplicationListener<Reposit
 			throw new IllegalStateException(String.format(PARAMETER_MISSING, method));
 		}
 
-		EventHandlerMethod handlerMethod = new EventHandlerMethod(parameterTypes[0], handler, method);
+		EventHandlerMethod handlerMethod = EventHandlerMethod.of(parameterTypes[0], handler, method);
 
 		if (LOG.isDebugEnabled()) {
 			LOG.debug("Annotated handler method found: {}", handlerMethod);
 		}
 
-		handlerMethods.add(eventType, handlerMethod);
+		List<EventHandlerMethod> events = handlerMethods.get(eventType);
+
+		if (events == null) {
+			events = new ArrayList<EventHandlerMethod>();
+		}
+
+		if (events.isEmpty()) {
+			handlerMethods.add(eventType, handlerMethod);
+			return;
+		}
+
+		events.add(handlerMethod);
+		Collections.sort(events);
+		handlerMethods.put(eventType, events);
 	}
 
-	static class EventHandlerMethod {
+	@ToString
+	@EqualsAndHashCode
+	@RequiredArgsConstructor
+	static class EventHandlerMethod implements Comparable<EventHandlerMethod> {
 
 		final Class<?> targetType;
 		final Method method;
 		final Object handler;
 
-		private EventHandlerMethod(Class<?> targetType, Object handler, Method method) {
-
-			this.targetType = targetType;
-			this.method = method;
-			this.handler = handler;
+		public static EventHandlerMethod of(Class<?> targetType, Object handler, Method method) {
 
-			ReflectionUtils.makeAccessible(this.method);
+			ReflectionUtils.makeAccessible(method);
+			return new EventHandlerMethod(targetType, method, handler);
 		}
 
-		/*
+		/* 
 		 * (non-Javadoc)
-		 * @see java.lang.Object#toString()
+		 * @see java.lang.Comparable#compareTo(java.lang.Object)
 		 */
 		@Override
-		public String toString() {
-			return String.format("EventHandlerMethod{ targetType=%s, method=%s, handler=%s }", targetType, method, handler);
+		public int compareTo(EventHandlerMethod o) {
+			return AnnotationAwareOrderComparator.INSTANCE.compare(this.method, o.method);
 		}
 	}
 }
